package com.lzy.crm.wechat.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.protobuf.ByteString;
import com.lzy.crm.base.date.DateUtil;
import com.lzy.crm.base.page.PageQuery;
import com.lzy.crm.domain.GamePlayerRecord;
import com.lzy.crm.domain.GameRecordTable;
import com.lzy.crm.proto.Group.CommMsg;
import com.lzy.crm.proto.RedisData.BigRecord;
import com.lzy.crm.proto.RedisData.BigRecordInfo;
import com.lzy.crm.proto.RedisData.PlayerInfo;
import com.lzy.crm.proto.RedisData.Record;
import com.lzy.crm.proto.RedisData.RecordInfo;
import com.lzy.crm.proto.RedisData.RecordInfo.Builder;
import com.lzy.crm.service.GamePlayerRecordService;
import com.lzy.crm.service.GameRecordTableService;
import com.lzy.crm.wechat.consts.GmConstants;


/**
 * 游戏字典服务器
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/gm")
public class GmGamePlayerRecordController {
	
	
	@Resource
	private GamePlayerRecordService gamePlayerRecordService;
	
	@Resource
	private GameRecordTableService gameRecordTableService;
	
	/**
	 * 玩家查看回放记录
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(path="/record",method=RequestMethod.GET,produces="text/html;charset=UTF-8")
	@ResponseBody
	public byte[] gameRecord(HttpServletRequest request,ModelMap model) {
		com.lzy.crm.proto.Group.CommMsg.Builder commMsg = CommMsg.newBuilder();
		String uid = request.getParameter("uid");
		PageQuery pageQuery = new PageQuery(1,30);
		List<GamePlayerRecord> playerRecordList= gamePlayerRecordService.getGamePlayerRecordList(uid,pageQuery);
		com.lzy.crm.proto.RedisData.Record.Builder record =Record.newBuilder();
		for(GamePlayerRecord playerRecord:playerRecordList){
			Builder recordInfo = RecordInfo.newBuilder();
			recordInfo.setRoomId(playerRecord.getRoomid());
			recordInfo.setNumGame(playerRecord.getNumber());
			recordInfo.setCreateTime(playerRecord.getCreateTime()!=null?DateUtil.formatDateWithTime(playerRecord.getCreateTime()):"");
			recordInfo.setGametype(playerRecord.getGametype());
			
			List<GamePlayerRecord> allgamePlayerRecordList= gamePlayerRecordService.getGamePlayerRecordListByRoomId(playerRecord.getRoomid().toString(), DateUtil.formatDateWithTime(playerRecord.getCreateTime()));
			for(GamePlayerRecord gamePlayerRecord: allgamePlayerRecordList){
				com.lzy.crm.proto.RedisData.PlayerInfo.Builder playerInfo = PlayerInfo.newBuilder();
				playerInfo.setName(gamePlayerRecord.getPlayerName());
				playerInfo.setScore(gamePlayerRecord.getScore());
				recordInfo.addPlayerInfos(playerInfo);
			}
			record.addRecordInfo(recordInfo);
		}
		commMsg.setCode(GmConstants.STATUS_CODE_SUCCESS);
		commMsg.setResp(record.build().toByteString());
		
		return commMsg.build().toByteArray();
	}
	
	/**
	 * 查看回放详情
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(path="/record2",method=RequestMethod.GET,produces="text/html;charset=UTF-8")
	@ResponseBody
	public byte[] gameRecord2(HttpServletRequest request,ModelMap model) {
		com.lzy.crm.proto.Group.CommMsg.Builder commMsg = CommMsg.newBuilder();
		String roomId = request.getParameter("roomId");
		String time = request.getParameter("time");
		
		com.lzy.crm.proto.RedisData.BigRecord.Builder  bigRecord=BigRecord.newBuilder();
	
		List<GameRecordTable> allgamePlayerRecordList = gameRecordTableService.getGameRecordTableList(roomId, time);
		
		for(GameRecordTable gameRecordTable: allgamePlayerRecordList){
			
			com.lzy.crm.proto.RedisData.BigRecordInfo.Builder bigRecordInfo =BigRecordInfo.newBuilder();
			
			bigRecordInfo.setIndexs(gameRecordTable.getIndexs());
			bigRecordInfo.setPlayback( ByteString.copyFrom((byte[]) gameRecordTable.getPlayback()));
			bigRecordInfo.setCreateTime(DateUtil.formatDateWithoutTime(gameRecordTable.getCreateTime()));
			bigRecordInfo.setEndTime(DateUtil.formatDateWithoutTime(gameRecordTable.getEndTime()));
			
			if(gameRecordTable.getUid1()!=null &&gameRecordTable.getUid1() !=0){
				com.lzy.crm.proto.RedisData.PlayerInfo.Builder playerInfo = PlayerInfo.newBuilder();
				playerInfo.setName(gameRecordTable.getNickname1());
				playerInfo.setScore(gameRecordTable.getScore1());
				bigRecordInfo.addPlayerInfos(playerInfo);
			}
			
			if(gameRecordTable.getUid2()!=null&&gameRecordTable.getUid2() !=0){
				com.lzy.crm.proto.RedisData.PlayerInfo.Builder playerInfo = PlayerInfo.newBuilder();
				playerInfo.setName(gameRecordTable.getNickname1());
				playerInfo.setScore(gameRecordTable.getScore1());
				bigRecordInfo.addPlayerInfos(playerInfo);
			}
			
			if(gameRecordTable.getUid3()!=null&&gameRecordTable.getUid3() !=0){
				com.lzy.crm.proto.RedisData.PlayerInfo.Builder playerInfo = PlayerInfo.newBuilder();
				playerInfo.setName(gameRecordTable.getNickname1());
				playerInfo.setScore(gameRecordTable.getScore1());
				bigRecordInfo.addPlayerInfos(playerInfo);
			}
			if(gameRecordTable.getUid4()!=null&&gameRecordTable.getUid4() !=0){
				com.lzy.crm.proto.RedisData.PlayerInfo.Builder playerInfo = PlayerInfo.newBuilder();
				playerInfo.setName(gameRecordTable.getNickname1());
				playerInfo.setScore(gameRecordTable.getScore1());
				bigRecordInfo.addPlayerInfos(playerInfo);
			}
			
			if(gameRecordTable.getUid5()!=null&&gameRecordTable.getUid5() !=0){
				com.lzy.crm.proto.RedisData.PlayerInfo.Builder playerInfo = PlayerInfo.newBuilder();
				playerInfo.setName(gameRecordTable.getNickname1());
				playerInfo.setScore(gameRecordTable.getScore1());
				bigRecordInfo.addPlayerInfos(playerInfo);
			}
			
			if(gameRecordTable.getUid6()!=null&&gameRecordTable.getUid6() !=0){
				com.lzy.crm.proto.RedisData.PlayerInfo.Builder playerInfo = PlayerInfo.newBuilder();
				playerInfo.setName(gameRecordTable.getNickname1());
				playerInfo.setScore(gameRecordTable.getScore1());
				bigRecordInfo.addPlayerInfos(playerInfo);
			}
			
			if(gameRecordTable.getUid7()!=null&&gameRecordTable.getUid7() !=0){
				com.lzy.crm.proto.RedisData.PlayerInfo.Builder playerInfo = PlayerInfo.newBuilder();
				playerInfo.setName(gameRecordTable.getNickname1());
				playerInfo.setScore(gameRecordTable.getScore1());
				bigRecordInfo.addPlayerInfos(playerInfo);
			}
			
			if(gameRecordTable.getUid8()!=null&&gameRecordTable.getUid8() !=0){
				com.lzy.crm.proto.RedisData.PlayerInfo.Builder playerInfo = PlayerInfo.newBuilder();
				playerInfo.setName(gameRecordTable.getNickname1());
				playerInfo.setScore(gameRecordTable.getScore1());
				bigRecordInfo.addPlayerInfos(playerInfo);
			}
			
			if(gameRecordTable.getUid9()!=null&&gameRecordTable.getUid9() !=0){
				com.lzy.crm.proto.RedisData.PlayerInfo.Builder playerInfo = PlayerInfo.newBuilder();
				playerInfo.setName(gameRecordTable.getNickname1());
				playerInfo.setScore(gameRecordTable.getScore1());
				bigRecordInfo.addPlayerInfos(playerInfo);
			}
			
			if(gameRecordTable.getUid10()!=null&&gameRecordTable.getUid10() !=0){
				com.lzy.crm.proto.RedisData.PlayerInfo.Builder playerInfo = PlayerInfo.newBuilder();
				playerInfo.setName(gameRecordTable.getNickname1());
				playerInfo.setScore(gameRecordTable.getScore1());
				bigRecordInfo.addPlayerInfos(playerInfo);
			}
			
			bigRecord.addBigrecordInfo(bigRecordInfo);
		}
		
		
		commMsg.setCode(GmConstants.STATUS_CODE_SUCCESS);
		commMsg.setResp(bigRecord.build().toByteString());
		
		return commMsg.build().toByteArray();
	}
	
	
	
	
	/**
	 * 群管理查看回放记录
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(path="group/game_record",method=RequestMethod.GET,produces="text/html;charset=UTF-8")
	@ResponseBody
	public byte[] cgameRecord2(HttpServletRequest request,ModelMap model) {
		com.lzy.crm.proto.Group.CommMsg.Builder commMsg = CommMsg.newBuilder();
		String group_id = request.getParameter("groupId");
		String uid = request.getParameter("uid");
		String time = request.getParameter("time");
		
		PageQuery pageQuery = new PageQuery(1,30);
		List<GamePlayerRecord> playerRecordList= gamePlayerRecordService.getGamePlayerRecordListByGroupId(group_id, pageQuery);
		com.lzy.crm.proto.RedisData.Record.Builder record =Record.newBuilder();
		
		for(GamePlayerRecord playerRecord:playerRecordList){
			Builder recordInfo = RecordInfo.newBuilder();
			recordInfo.setRoomId(playerRecord.getRoomid());
			recordInfo.setNumGame(playerRecord.getNumber());
			recordInfo.setCreateTime(playerRecord.getCreateTime()!=null?DateUtil.formatDateWithTime(playerRecord.getCreateTime()):"");
			recordInfo.setGametype(playerRecord.getGametype());
			
			List<GamePlayerRecord> allgamePlayerRecordList= gamePlayerRecordService.getGamePlayerRecordListByRoomId(playerRecord.getRoomid().toString(), DateUtil.formatDateWithTime(playerRecord.getCreateTime()));
			for(GamePlayerRecord gamePlayerRecord: allgamePlayerRecordList){
				com.lzy.crm.proto.RedisData.PlayerInfo.Builder playerInfo = PlayerInfo.newBuilder();
				playerInfo.setName(gamePlayerRecord.getPlayerName());
				playerInfo.setScore(gamePlayerRecord.getScore());
				recordInfo.addPlayerInfos(playerInfo);
			}
			record.addRecordInfo(recordInfo);
		}
		commMsg.setCode(GmConstants.STATUS_CODE_SUCCESS);
		commMsg.setResp(record.build().toByteString());
		
		return commMsg.build().toByteArray();
	}

}
